package com.example.lym.syappandroid;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import model.Alarmtask;
import model.TaskalarmAdapter;
import model.User;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import regular.IRegulation;

public class TaskalarmActivity extends AppCompatActivity {

    private List<Alarmtask> alarmtaskList = new ArrayList<>();
    private ListView lv_alarmtasks;
    private TextView tv_alarm_all;
    private TextView tv_alarm_task;
    private ImageView iv_alarm_logout;
    private ImageView iv_totask;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_taskalarm);
        ActivityCollector.addActivity(TaskalarmActivity.this);
        Init();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ActivityCollector.removeActivity(TaskalarmActivity.this);
    }

    /**
     * 初始化控件和事件
     */
    private void Init(){
        //初始化任务列表
        InitAlarmtaskList();
        //初始化切换标签
        tv_alarm_all = (TextView) findViewById(R.id.tv_alarm_all);
        tv_alarm_task = (TextView) findViewById(R.id.tv_alarm_task);
        tv_alarm_all.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(TaskalarmActivity.this,AlarmActivity.class);
                startActivity(intent);
            }
        });
        //TODO 初始化任务-提醒切换标签
        iv_totask = (ImageView)findViewById(R.id.iv_totask);
        iv_totask.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(TaskalarmActivity.this,TaskActivity.class);
                startActivity(intent);
            }
        });
        //初始化退出标签
        iv_alarm_logout = (ImageView) findViewById(R.id.iv_alarm_logout);
        iv_alarm_logout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //退出事件
                ActivityCollector.finishAll();
            }
        });
    }

    /**
     * 初始化任务列表
     */
    private void InitAlarmtasks() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                lv_alarmtasks = (ListView)findViewById(R.id.lv_alarmtasks);
                TaskalarmAdapter adapter = new TaskalarmAdapter(TaskalarmActivity.this,R.layout.taskalarm_item,alarmtaskList);
                lv_alarmtasks.setAdapter(adapter);
                lv_alarmtasks.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        Alarmtask alarmtask = alarmtaskList.get(position);
                        Intent intent = new Intent(TaskalarmActivity.this,AlarmlistActivity.class);
                        intent.putExtra("t_id",alarmtask.getT_id());
                        intent.putExtra("t_name",alarmtask.getT_name());
                        startActivity(intent);
                    }
                });
            }
        });
    }

    /**
     * 初始化数据列表
     */
    private void InitAlarmtaskList(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    OkHttpClient client = new OkHttpClient();
                    RequestBody requestBody = new FormBody.Builder().add("u_id", User.getU_id()+"").build();
                    Request request = new Request.Builder().url(IRegulation.URL+"task/getTaskList").post(requestBody).build();
                    Response response = null;
                    response = client.newCall(request).execute();
                    String str = response.body().string();
                    JSONObject jsonObject = JSONObject.fromObject(str);
                    //获得List<Tasks>
                    JSONArray jsonArray = jsonObject.getJSONArray("data");
                    //根据t_id获取alarms
                    for(int i = 0 ; i<jsonArray.size();i++){
                        JSONObject temp = jsonArray.getJSONObject(i);
                        int t_id = temp.getInt("t_id");
                        String t_name = temp.getString("t_name");
                        NewAlarmtask(t_id , t_name);
                    }
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    InitAlarmtasks();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }).start();
    }

    private void NewAlarmtask(final int t_id,final String t_name) {
        try {
            OkHttpClient client = new OkHttpClient();
            RequestBody requestBody = new FormBody.Builder().add("u_id", User.getU_id() + "").add("t_id",t_id+"").build();
            Request request = new Request.Builder().url(IRegulation.URL + "alarm/getlistbytaskid").post(requestBody).build();
            Response response = null;
            response = client.newCall(request).execute();
            String str = response.body().string();
            JSONObject jsonObject = JSONObject.fromObject(str);
            JSONArray jsonArray = jsonObject.getJSONArray("data");
            //增加
            for(int i=0;i<jsonArray.size();i++){
                JSONObject temp = jsonArray.getJSONObject(i);
                if(temp.getInt("a_isread")==0){
                    Alarmtask a = new Alarmtask();
                    a.setT_name(t_name);
                    a.setT_id(t_id);
                    a.setT_unread(true);
                    alarmtaskList.add(a);
                    return;
                }
            }
            Alarmtask a = new Alarmtask();
            a.setT_name(t_name);
            a.setT_id(t_id);
            a.setT_unread(false);
            alarmtaskList.add(a);
            return;
        }catch (IOException e){
            e.printStackTrace();
        }
    }
}
